﻿using System.Windows.Forms;
using LetiLib.Device;
using LetiLib.Utils;

namespace LedCenter.GUI
{
    public partial class DeviceDetails : UserControl
    {
        public DeviceDetails()
        {
            InitializeComponent();
            Log.DataLogged += new Logger.LogHandler(Log_DataLogged);
        }
        delegate void DataLogCallBack(string stSock);
        void Log_DataLogged(string content)
        {
            if (this.txtLogs.InvokeRequired)
            {
                DataLogCallBack d = new DataLogCallBack(Log_DataLogged);
                this.Invoke(d, new object[] { content });
            }
            else
            {
            txtLogs.Text += content;
            txtLogs.SelectionStart = txtLogs.Text.Length;
            txtLogs.ScrollToCaret();
            }
        }

        private BaseDevice _device_info;
        Logger Log = Logger.Instance;

        public BaseDevice DeviceInfo 
        {
            get { return _device_info; }
            set
            {
                if (value!= null)
                {
                    _device_info = value;
                    populateDevice();
                }
            }
        }

        internal void populateDevice()
        {
            Log.setActiveIndex(_device_info.IMEI);
            if (_device_info.Type == DEVICE_TYPE.SMS)
            {
                populateSmsDevice((SMSDevice)_device_info);
            }
            else if (_device_info.Type == DEVICE_TYPE.TCP)
            {
                populateTcpDevice((TCPDevice)_device_info);
            }
        }

        private void populateTcpDevice(TCPDevice dev)
        {
            tbName.Text = dev.Name;

            //busControl.Visible = false;
            ledPageControl.Visible = true;
            ledPageControl.Enabled = dev.isActive;
            
            ledPageControl.Visible = true;
            ledPageControl.Enabled = true;
            ledPageControl.DataSource = dev.pages;

            tbIMEI.Text = dev.IMEI;
            tbType.Text = "TCP";
            tbData.Text = dev.IP;
            tbExtras.Text = dev.Port.ToString();
            tbPass.Text = dev.Password;
            lbData.Text = "ID Address";
            lbExtra.Text = "Port";
            this.pbImage.Image = global::LedCenter.Properties.Resources.tcp;
        }

        private void populateSmsDevice(SMSDevice dev)
        {
            ledPageControl.Visible = false;
            ledPageControl.Enabled = false;

            tbName.Text = dev.Name;
            tbIMEI.Text = dev.IMEI;
            tbType.Text = "SMS";
            tbData.Text = dev.PhoneNumber;
            tbExtras.Text = dev.Mode == SMS_MODE.PLAINTEXT ? "PLAIN" : "PDU";
            tbPass.Text = dev.Password;
            lbData.Text = "Phone number";
            lbExtra.Text = "Mode";
            this.pbImage.Image = global::LedCenter.Properties.Resources.sms128x128;
        }

        private void btnChangePass_Click(object sender, System.EventArgs e)
        {

            DeviceForm dForm = new DeviceForm(DeviceInfo);
            if (dForm.ShowDialog() == DialogResult.OK)
            {
                // TODO: save password

            }
        }
    }
}

